<template>
  <div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.common.button.add') }}</a-button>
      <a-button @click="handleSelectTable" icon="database">{{$t('m.eform.apptable.other.load_database')}}</a-button>
      <a-button @click="batchDel" type="default" icon="delete">{{ $t('m.common.button.delete') }}</a-button>

      <div style="float: right">
        <a-input-search style="width: 200px;" :placeholder="$t('m.common.label.guan-jian-zi')" enter-button @search="onSearch"></a-input-search>
      </div>
    </div>

    <div>
    <!-- table区域-begin -->
    <a-table
      ref="table"
      size="middle"
      rowKey="id"
      bordered
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange"
    >
      <template slot="serialNum" slot-scope="text,record,index">
        {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
      </template>

      <template slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">{{ $t('m.common.button.update') }}</a>
        <a-divider type="vertical"/>

        <template v-if="record.isSync == '0'">
          <a-popconfirm :title="$t('m.eform.apptable.verify.confirm_create_table')"
                        @confirm="() => handleCreateTable(record.id)">
            <a>{{$t('m.eform.apptable.other.create_table')}}</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
        </template>

        <template v-if="record.isSync == '2'">
          <a-popconfirm :title="$t('m.eform.apptable.verify.confirm_sync_table')"
                        @confirm="() => handleUpdateTable(record.id)">
            <a>{{$t('m.eform.apptable.other.sync_table')}}</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
        </template>

        <a-dropdown>
          <a class="ant-dropdown-link">{{$t('m.common.button.more')}}
            <ych-icon type="down"/>
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.id)">
                <a>{{ $t('m.common.button.delete') }}</a>
              </a-popconfirm>
            </a-menu-item>
            <a-menu-item>
              <a @click="autoCreateERModel(record.id)">{{$t('m.eform.apptable.other.create_er')}}</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="autoCreateObject(record.id)">{{$t('m.eform.apptable.other.create_er_form')}}</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </template>

    </a-table>
    </div>
    <actAppTable-modal ref="modalForm" @ok="modalFormOk" :groupId="groupId"/>

    <select-db-table-dialog ref="selectDbTableDialog" @selectFinished="selectDbTableOk"/>

    <a-modal
      :maskClosable="false"
      :keyboard="false"
      :title="$t('m.eform.apptable.verify.confirm_handle_title')"
      :visible="createObjVisible"
      width="300px"
      @cancel="handleCreateObjCancel">

      <p>
        {{$t('m.eform.apptable.verify.confirm_handle_type')}}
      </p>

      <template slot="footer">
        <a-button key="cover" @click="handleCover">{{$t('m.eform.apptable.other.handleCover')}}</a-button>
        <a-button key="ignore" @click="handleIgnore">{{$t('m.eform.apptable.other.handleIgnore')}}</a-button>
        <a-button key="back" @click="handleCreateObjCancel">{{$t('m.eform.apptable.other.handleCancel')}}</a-button>
      </template>
    </a-modal>
    <a-modal
      :maskClosable="false"
      :keyboard="false"
      :title="$t('m.eform.apptable.verify.confirm_handle_title')"
      :visible="createERModelVisible"
      width="300px"
      @cancel="handleCreateObjCancel">

      <p>
        {{$t('m.eform.apptable.verify.confirm_handle_type')}}
      </p>

      <template slot="footer">
        <a-button key="cover" @click="handleCoverERModel">{{$t('m.eform.apptable.other.handleCover')}}</a-button>
        <a-button key="ignore" @click="handleIgnoreERModel">{{$t('m.eform.apptable.other.handleIgnore')}}</a-button>
        <a-button key="back" @click="handleCreateObjCancel">{{$t('m.eform.apptable.other.handleCancel')}}</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>

  import {YcListMixin} from '@/mixins/YcListMixin'
  import {filterMultiDictText} from '@/components/dict/YchDictSelectUtil'
  import {postAction} from '@/api/manage'
  import ActAppTableModal from './modules/ActAppTableModal'
  import SelectDbTableDialog from './select/dialog/SelectDbTableDialog'
  import {EFormConstant} from './EFormConstant'
  import YchConstant from "@/api/YchConstant";
  import LocaleUtil from "../../utils/LocaleUtil";

  export default {
    name: 'ActAppTableList',
    mixins: [YcListMixin],
    components: {
      ActAppTableModal,
      SelectDbTableDialog
    },
    props: {
      groupId: {
        type: String,
        required: true
      },
    },
    data() {
      return {
        queryParam: {
          groupId: this.groupId
        },
        isorter: {
          column: 'orderNum',
          order: 'asc',
        },
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.eform.apptable.entity.tableName'),
            align: 'center',
            dataIndex: 'tableName',
            sorter: (a, b) => a.tableName.slice(0,1).charCodeAt(0)-b.tableName.slice(0,1).charCodeAt(0),//表名称排序
          },
          {
            title: this.$t('m.eform.apptable.entity.tableDesc'),
            align: 'center',
            dataIndex: 'tableDesc'
          },
          {
            title: this.$t('m.eform.apptable.entity.dsName'),
            align: 'center',
            dataIndex: 'dsName'
          },
          {
            title: this.$t('m.eform.apptable.entity.isSync'),
            align: 'center',
            width: 120,
            dataIndex: 'isSync',
            customRender: (text) => {
              if (!text) {
                return ''
              } else {
                return filterMultiDictText(EFormConstant.isSync, text + '')
              }
            }
          },
          {
            title: this.$t('m.common.label.sort'),
            align: 'center',
            dataIndex: 'orderNum',
            width: 80,
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            width: 300,
            scopedSlots: {customRender: 'action'},
          }
        ],
        url: {
          list: YchConstant.PREFIX_EFORM + '/manage/actAppTable/list',
          delete: YchConstant.PREFIX_EFORM + '/manage/actAppTable/delete',
          deleteBatch: YchConstant.PREFIX_EFORM + '/manage/actAppTable/deleteBatch',
          createTable: YchConstant.PREFIX_EFORM + '/manage/actAppTable/createTable',
          syncTable: YchConstant.PREFIX_EFORM + '/manage/actAppTable/syncTable',
          autoCreateObject: YchConstant.PREFIX_EFORM + '/manage/actAppTable/autoCreateObject',
          getDbTables: YchConstant.PREFIX_EFORM + '/manage/actAppTable/getDbTables',
          getTableMeta: YchConstant.PREFIX_EFORM + '/manage/actAppTable/getTableMeta',
          copyModelFromDb: YchConstant.PREFIX_EFORM + '/manage/actAppTable/copyModelFromDb',
        },
        createObjVisible: false,
        createERModelVisible: false,
        currentId: null,
      }
    },
    methods: {
      handleSelectTable() {
        this.$refs.selectDbTableDialog.radio = false
        this.$refs.selectDbTableDialog.visible = true
      },
      selectDbTableOk(selectedRowKeys, selectionRows) {
        const tableName = selectionRows.map(vo => vo.tableName).join(',')
        const dsCode = selectionRows[0].dsCode
        postAction(this.url.copyModelFromDb + '?tableName=' + tableName + '&dsCode=' + dsCode + '&groupId=' + (this.groupId || ''), {}).then((res) => {
          if (res.success) {
            this.messageUtil.success(this.$t('m.common.tip.operate_success'))
          } else {
            this.messageUtil.warning(res.message)
          }
          this.loadData()
        }).finally(() => {
        })
      },
      handleCreateTable(id) {
        postAction(this.url.createTable + '?id=' + id, {}).then((res) => {
          if (res.success) {
            if (res.result == 'ok') {
              this.messageUtil.success(this.$t('m.eform.apptable.verify.create_table_success'))
              this.autoCreateObject(id)
            } else if (res.result == 'created') {
              this.messageUtil.warning(this.$t('m.eform.apptable.verify.table_exist'))
            }
            this.loadData()
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleUpdateTable(id) {
        postAction(this.url.syncTable + '?id=' + id, {}).then((res) => {
          if (res.success) {
            if (res.result == 'ok') {
              this.messageUtil.success(this.$t('m.eform.apptable.verify.sync_success'))
            } else if (res.result == 'noCreated') {
              this.messageUtil.warning(this.$t('m.eform.apptable.verify.table_not_exist'))
            } else if (res.result == 'cannotOperate') {
              this.messageUtil.warning(this.$t('m.eform.apptable.verify.system_table_no_update'))
            }
            this.loadData()
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      autoCreateObject(id) {
        const that = this
        this.currentId = id
        this.$confirm({
          content: this.$t('m.eform.apptable.verify.confirm_create_er_form'),
          onOk() {
            that.autoCreateObjectByOpType(id)
          },
          onCancel() {
          },
        })
      },
      autoCreateObjectByOpType(id, opType) {
        let url = this.url.autoCreateObject + '?id=' + id
        if (opType) {
          url += '&opType=' + opType
        }
        postAction(url, {}).then((res) => {
          if (res.success && res.result == 'ok') {
            this.messageUtil.success(this.$t('m.common.tip.operate_success'))
          } else if (res.success && res.result == 'repeatCode') {
            this.createObjVisible = true
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleCover() {
        this.autoCreateObjectByOpType(this.currentId, 'cover')
        this.createObjVisible = false
      },
      handleIgnore() {
        this.autoCreateObjectByOpType(this.currentId, 'ignore')
        this.createObjVisible = false
      },
      handleCreateObjCancel() {
        this.createObjVisible = false
        this.createERModelVisible = false
      },
      autoCreateERModel(id) {
        const that = this
        this.currentId = id
        this.$confirm({
          content:this.$t('m.eform.apptable.verify.confirm_create_er'),
          onOk() {
            that.autoCreateERModelByOpType(id)
          },
          onCancel() {
          },
        })
      },
      autoCreateERModelByOpType(id, opType) {
        let url = this.url.autoCreateObject + '?id=' + id + '&onlyERModel=1'
        if (opType) {
          url += '&opType=' + opType
        }
        postAction(url, {}).then((res) => {
          if (res.success && res.result == 'ok') {
            this.messageUtil.success(this.$t('m.common.tip.operate_success'))
          } else if (res.success && res.result == 'repeatCode') {
            this.createERModelVisible = true
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      handleCoverERModel() {
        this.autoCreateERModelByOpType(this.currentId, 'cover')
        this.createERModelVisible = false
      },
      handleIgnoreERModel() {
        this.autoCreateERModelByOpType(this.currentId, 'ignore')
        this.createERModelVisible = false
      },
      modalFormOk() {
        // 新增/修改 成功时，重载列表
        this.loadData()
        // 刷新多语言
        LocaleUtil.refreshLocale()
      },
      afterBatchDel() {
        // 刷新多语言
        LocaleUtil.refreshLocale()
      },
      afterHandleDelete() {
        // 刷新多语言
        LocaleUtil.refreshLocale()
      },
    }
  }
</script>
<style lang="less" scoped>
</style>
